package com.hkemmm.control;

import com.hkemmm.model.UserModel;
import com.hkemmm.utils.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String userName = request.getParameter("userName");
        String userPsw = request.getParameter("userPsw");

        request.setAttribute("userName",userName);
        //UserModel user = new UserModel();
        if(MyUtils.getInstence(UserModel.class).canLogin(userName,userPsw)){
            //重定向
            //不可以访问站点安全目录，不是同一个request，无法携带数据
            response.sendRedirect("views/logined.jsp");
        }else {
            //转发
            //可以访问站点安全目录，是同一个request，可以携带数据
            request.getRequestDispatcher("WEB-INF/viewpages/login.jsp").forward(request,response);
        }
    }
}
